//
//  map.h
//  chrisnova
//
//  Created by Chris St. Pierre on 3/29/13.
//  Copyright (c) 2013 Nova Saint. All rights reserved.
//

#ifndef __chrisnova__map__
#define __chrisnova__map__

#include <iostream>
template<class name, class object>
class map
{
public:
    map()
    {
        head = NULL;
        m_size = 0;
    }
    ~map();
    bool empty() const;
    {return(head == NULL); }
    int size() const;
    {return m_size;};
    bool insert(const &name,const &object);
    bool erase(const &name);
    object* find(const &name);
    bool swap(const &name, const &object);
    void clear();
private:
    struct node
    {
        name m_name;
        object m_object;
        node* leftNode;
        node* rightNode;
    };
    int m_size;
    node* head;
    node* recursiveFinder(name title,node* head);
    
};

template <class name, class object>
bool map<name,object>map::insert(const &name, const &object)
{
    node* find = recursiveFinder(head);

    if(*name< find){
        *head.leftNode = new node;
        *head.leftNode.name = name;
        *head.leftNode.object = object;
    }
    
    if(*name> find)
    {
        *head.rightNode = new node;
        *head.rightNode.name = name;
        *head.rightNode.object = object;
    }
    if(find == NULL)
        return NULL;
    if(*name == find)
    {
        return head;
    }
}
    
}

template<class name, class object>
bool map<name,object>map:: erase(const &name){
    node* find = recursiveFinder(head.name);
    if(*name == find){
        delete find;
    }
}

template <class name, class object>
node* map<name,object>map::recursiveFinder(name title, node* head)
{
    if (head == null)
        return null;
    if( *head.m_name == title)
        return head;
    
    if(*head.m_name < title)
    {
        if(*head.leftNode == NULL)
            return head;
        return recursiveFinder(title,*head.leftNode);
    }
    if(*head.m_name > title){
        if(*head.rightNode == NULL)
            return head;
        return recursiveFinder(title,*head.rightNode);
    }

}
#endif /* defined(__chrisnova__map__) */
